package com.think.teaching.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.think.teaching.mapper.ChapterMapper;
import com.think.teaching.model.Chapter;
import com.think.teaching.model.ExamResult;
import com.think.teaching.service.ChapterService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 章 服务实现类
 * </p>
 *
 * @author think
 * @since 2020-08-12
 */
@Service
public class ChapterServiceImpl extends BaseServiceImpl<ChapterMapper, Chapter> implements ChapterService {

	@Override
	public Chapter AddChapter(Chapter chapter) {
		chapter.setIndex(baseMapper.getMaxIndexOfCurrentChapterLayer(chapter.getCourseId(), chapter.getParent() == null?-1:chapter.getParent()) + 1);
		if (this.save(chapter)) {
			return chapter;
		}

		return null;
	}

	@Override
	public Chapter UpdateChapter(Chapter chapter) {
		if (this.updateById(chapter)) {
			return chapter;
		}
		return null;
	}

	@Override
	public Chapter GetChapter(Integer id) {

		return baseMapper.getChapterDetails(id);
	}

	public Chapter CheckChapterExist(Integer id) {
		QueryWrapper<Chapter> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("id", id).ne("delflag", 1);
		return this.getOne(queryWrapper);
	}

	public int DeleteLesson(Integer id, Integer lessonType) {
		return baseMapper.DeleteLesson(id, lessonType);
	}

	public int ChangeLessonState(Integer id, Integer lessonType, Integer state) {
		return baseMapper.ChangeLessonState(id, lessonType, state);
	}

	@Override
	public List<Chapter> ListChapterSearch(String key, Boolean withoutBan) {
		QueryWrapper<Chapter> queryWrapper = new QueryWrapper<>();
		queryWrapper.ne("delflag", 1).like("chapter_name", key);
		if (withoutBan) {
			queryWrapper.eq("state", 1);
		}
		return list(queryWrapper);

	}

	@Override
	public Chapter SetChapterState(Integer id, Integer state) {
		UpdateWrapper<Chapter> queryWrapper = new UpdateWrapper<>();
		queryWrapper.eq("id", id).ne("delflag", 1).set("state", state);
		this.update(queryWrapper);
		return getById(id);
	}

	@Override
	public IPage<Chapter> ListChapterWithTagAndType(Integer count, Integer page, Integer type, Integer tag,
			Boolean withoutBan) {
		QueryWrapper<Chapter> queryWrapper = new QueryWrapper<>();
		queryWrapper.ne("delflag", 1);
		if (withoutBan) {
			queryWrapper.eq("state", 1);
		}
		IPage<Chapter> ipage = new Page<>(page, count);
		return this.page(ipage);
	}

	public Integer getLessonDetails(Integer id) {
		return id;
	}

	public List<Chapter> GetChapterWithCourseId(Integer id, Boolean withoutBan) {
		QueryWrapper<Chapter> queryWrapper = new QueryWrapper<>();
		queryWrapper.ne("delflag", 1).eq("course_id", id);
		if (withoutBan) {
			queryWrapper.eq("state", 1);
		}
		return this.list(queryWrapper);
	}

	/**
	 * 删除和指定的courseId相关的章节
	 * 
	 * @param courseId
	 * @return
	 */
	public Boolean DeleteRecordsByCourseId(Integer courseId) {
		UpdateWrapper<Chapter> queryWrapper = new UpdateWrapper<Chapter>();
		queryWrapper.eq("course_id", courseId).set("delflag", 1);
		return this.update(queryWrapper);
	}

	public Boolean MoveChapter(Chapter chapter, Boolean up) {
		QueryWrapper<Chapter> queryWrapper = new QueryWrapper<>();
		queryWrapper.ne("delflag", 1).eq("course_id", chapter.getCourseId());
		if (chapter.getParent() == null) {
			queryWrapper.isNull("parent");//, chapter.getParent());
		}
		else
			queryWrapper.eq("parent", chapter.getParent());
		queryWrapper.orderByAsc("`index`");
		List<Chapter> chapters = this.list(queryWrapper);
		if (chapters.size() <= 1) {
			return true;
		}
		Integer oringinIndex = chapter.getIndex();
		Integer index = -1;
		for (int i = 0; i < chapters.size(); i++) {
			if (chapter.getId().equals(chapters.get(i).getId())) {
				index = i;
				break;
			}
		}
		if (up && index == 0) {
			return true;
		}
		else if (!up && index == chapters.size() -1){
			return true;
		}
		Chapter exChapter = up?chapters.get(index -1):chapters.get(index +1);
		chapters.get(index).setIndex(exChapter.getIndex());
		exChapter.setIndex(oringinIndex);
		List<Chapter> entityList = new ArrayList<Chapter>();
		entityList.add(exChapter);
		entityList.add(chapters.get(index));
		return this.updateBatchById(entityList);
	}

}
